/*  
 *  ------   [802_09a] - Use of Expansion board     -------- 
 *  
 *  Explanation: This example shows how to use two XBee modules
 *  simultaneously in Waspmote using the Expansion board. Two 
 *  different objects are created in this code, each one is used 
 *  for each module. ONe is connected in the standard socket (SOCKET0)
 *  and the other one is connected to the Expansion board (SOCKET1)
 *  
 *  Copyright (C) 2012 Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details. 
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 *  
 *  Version:           0.1 
 *  Design:            David Gascón 
 *  Implementation:    Yuri Carmona
 */

#include <WaspXBee802.h>

// define objects
WaspXBee802 xbee0 = WaspXBee802();
WaspXBee802 xbee1 = WaspXBee802();

char data0[100];
char data1[100];

//Pointer to an XBee packet structure 
packetXBee* packet0;  
packetXBee* packet1; 

// sequence number
int seq=0;


void setup()
{
  // init USB port
  USB.ON();
  USB.println(F("802_09a example"));

  // initialize object in SOCKET0    
  xbee0.ON(SOCKET0);

  // initiliaze object in SOCKET1
  xbee1.ON(SOCKET1);  

}

void loop()
{ 
  // increment sequence number 
  seq++; 

  // Prepare the payload which is going to be sent within the packet
  sprintf(data0,"SOCKET0 -> seq:%d;free:%d;battery level:%d%c\r\n", seq, freeMemory(), PWR.getBatteryLevel(),'%');
  sprintf(data1,"SOCKET1 -> seq:%d;free:%d;battery level:%d%c\r\n", seq, freeMemory(), PWR.getBatteryLevel(),'%');



  /*** SOCKET0 ***/
  // Set params to send
  packet0=(packetXBee*) calloc(1,sizeof(packetXBee)); // Memory allocation
  packet0->mode=UNICAST; // Choose transmission mode: UNICAST or BROADCAST

  // Set destination XBee parameters
  xbee0.setDestinationParams(packet0, "0013A2004052414C", data0, MAC_TYPE); 

  // Send XBee packet
  xbee0.sendXBee(packet0);

  // Check TX flag
  if( xbee0.error_TX == 0 ) 
  {
    USB.println(F("ok"));
  }
  else 
  {
    USB.println(F("error")); 
  }

  // Free variables
  free(packet0);
  packet0=NULL;

  delay(1000);



  /*** SOCKET1 ***/
  // Set params to send
  packet1=(packetXBee*) calloc(1,sizeof(packetXBee)); // Memory allocation
  packet1->mode=UNICAST; // Choose transmission mode: UNICAST or BROADCAST

  // Set destination XBee parameters
  xbee1.setDestinationParams(packet1, "0013A2004052414C", data1, MAC_TYPE); 

  // Send XBee packet
  xbee1.sendXBee(packet1);

  // Check TX flag
  if( xbee1.error_TX == 0 ) 
  {
    USB.println(F("ok"));
  }
  else 
  {
    USB.println(F("error")); 
  }

  // Free variables
  free(packet1);
  packet1=NULL;

  // Wait for five seconds
  delay(5000);
}




